package org.example.struct.array;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/12/25 10:03
 * @apiNote
 */

public class Test00594FindLHS {

    public static int findLHS(int[] nums) {
        Arrays.sort(nums);
        int begin = 0;
        int res = 0;
        for (int end = 0; end < nums.length; end++) {
            while (nums[end] - nums[begin] > 1){
                begin++;
            }
            if (nums[end] - nums[begin] == 1) {
                res = Math.max(res, end - begin + 1);
            }
        }
        return res;
    }

    public static int findLHS1(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        int res = 0;
        for(int num:nums){
            map.put(num,map.getOrDefault(num,0)+1);
        }
        for(int key:map.keySet()){
            if(map.containsKey(key+1)){
                res = Math.max(res,map.get(key)+map.get(key+1));
            }
        }
        return res;
    }
}
